/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package org.alvin.api.ui;

import java.text.MessageFormat;
import javax.swing.JOptionPane;

/**
 *
 * @author tangzhichao
 */
public class StringPanel extends javax.swing.JPanel {

    /**
     * Creates new form StringPanel
     */
    public StringPanel() {
        initComponents();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        buttonGroup1 = new javax.swing.ButtonGroup();
        jTabbedPane1 = new javax.swing.JTabbedPane();
        jScrollPane1 = new javax.swing.JScrollPane();
        inputText = new javax.swing.JTextArea();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jTabbedPane2 = new javax.swing.JTabbedPane();
        jScrollPane2 = new javax.swing.JScrollPane();
        outPutText = new javax.swing.JTextArea();
        clazzNameText = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        trimBox = new javax.swing.JRadioButton();
        replaceAllBox = new javax.swing.JRadioButton();
        noTrimBox = new javax.swing.JRadioButton();
        jLabel2 = new javax.swing.JLabel();
        collectionNameText = new javax.swing.JTextField();
        jButton3 = new javax.swing.JButton();
        jButton4 = new javax.swing.JButton();
        jLabel3 = new javax.swing.JLabel();
        qianzhuiText = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        houZhuiText = new javax.swing.JTextField();
        jButton5 = new javax.swing.JButton();
        jLabel5 = new javax.swing.JLabel();
        jButton7 = new javax.swing.JButton();
        textSplitColumns = new javax.swing.JTextField();
        textBoxSplit = new javax.swing.JTextField();

        inputText.setColumns(20);
        inputText.setRows(5);
        jScrollPane1.setViewportView(inputText);

        jTabbedPane1.addTab("输入字符串", jScrollPane1);

        jButton1.setText("转为数组");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jButton2.setText("转为对象");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        outPutText.setColumns(20);
        outPutText.setRows(5);
        jScrollPane2.setViewportView(outPutText);

        jTabbedPane2.addTab("输出结果", jScrollPane2);

        jLabel1.setText("类名：");

        buttonGroup1.add(trimBox);
        trimBox.setText("去除外围空格");

        buttonGroup1.add(replaceAllBox);
        replaceAllBox.setText("去除所有空格");

        buttonGroup1.add(noTrimBox);
        noTrimBox.setSelected(true);
        noTrimBox.setText("不去除空格");

        jLabel2.setText("集合名：");

        jButton3.setText("转集合");
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });

        jButton4.setText("首字母小写");
        jButton4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton4ActionPerformed(evt);
            }
        });

        jLabel3.setText("前缀：");

        jLabel4.setText("后缀：");

        jButton5.setText("多行连接");
        jButton5.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton5ActionPerformed(evt);
            }
        });

        jLabel5.setText("分隔符：");

        jButton7.setText("分割取前");
        jButton7.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton7ActionPerformed(evt);
            }
        });

        textSplitColumns.setText("1,2");

        textBoxSplit.setText(",");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel3)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(qianzhuiText, javax.swing.GroupLayout.PREFERRED_SIZE, 139, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(jLabel4)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(houZhuiText, javax.swing.GroupLayout.PREFERRED_SIZE, 199, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(jButton5)
                        .addGap(375, 375, 375))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addGroup(layout.createSequentialGroup()
                                        .addComponent(trimBox)
                                        .addGap(33, 33, 33)
                                        .addComponent(replaceAllBox))
                                    .addGroup(layout.createSequentialGroup()
                                        .addComponent(jLabel2)
                                        .addGap(18, 18, 18)
                                        .addComponent(collectionNameText, javax.swing.GroupLayout.PREFERRED_SIZE, 97, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                        .addComponent(jButton3)))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                    .addGroup(layout.createSequentialGroup()
                                        .addComponent(noTrimBox)
                                        .addGap(26, 26, 26)
                                        .addComponent(jButton1))
                                    .addGroup(layout.createSequentialGroup()
                                        .addGap(21, 21, 21)
                                        .addComponent(jLabel5, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(textBoxSplit, javax.swing.GroupLayout.PREFERRED_SIZE, 43, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addGap(18, 18, 18)
                                        .addComponent(textSplitColumns, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE)))
                                .addGap(18, 18, 18)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(layout.createSequentialGroup()
                                        .addComponent(jButton4)
                                        .addGap(18, 18, 18)
                                        .addComponent(jLabel1)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                        .addComponent(clazzNameText, javax.swing.GroupLayout.PREFERRED_SIZE, 103, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addGap(18, 18, 18)
                                        .addComponent(jButton2))
                                    .addComponent(jButton7))
                                .addGap(0, 0, Short.MAX_VALUE))
                            .addComponent(jTabbedPane2, javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jTabbedPane1))
                        .addContainerGap())))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jTabbedPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 204, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton1)
                    .addComponent(trimBox)
                    .addComponent(replaceAllBox)
                    .addComponent(noTrimBox)
                    .addComponent(jButton4)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jButton2)
                        .addComponent(clazzNameText, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jLabel1)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(collectionNameText, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButton3)
                    .addComponent(jLabel5)
                    .addComponent(jButton7)
                    .addComponent(textSplitColumns, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(textBoxSplit, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(qianzhuiText, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel4)
                    .addComponent(houZhuiText, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButton5))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jTabbedPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 347, Short.MAX_VALUE))
        );

        layout.linkSize(javax.swing.SwingConstants.VERTICAL, new java.awt.Component[] {jButton3, jButton7, jLabel5, textBoxSplit, textSplitColumns});

    }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        // TODO add your handling code here:
        this.outPutText.setText("");
        String result = getStringReuslt(false);
        this.outPutText.setText(result);
    }//GEN-LAST:event_jButton1ActionPerformed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        // TODO add your handling code here:
        if (this.clazzNameText.getText().trim().isEmpty()) {
            return;
        }
        this.outPutText.setText("");
        String result = getObjectResult(false);
        this.outPutText.setText(result);
    }//GEN-LAST:event_jButton2ActionPerformed

    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
        // TODO add your handling code here:
        if (this.collectionNameText.getText().trim().isEmpty()) {
            return;
        }
        this.outPutText.setText("");
        String text = inputText.getText();
        String[] array = text.split("\r|\n|\r\n");
        if (!this.clazzNameText.getText().trim().isEmpty()) {
            this.outPutText.setText(this.getObjectResult(true));
        } else {
            this.outPutText.setText(this.getStringReuslt(true));
        }
    }//GEN-LAST:event_jButton3ActionPerformed

    private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton4ActionPerformed
        // TODO add your handling code here:
        String result = "";
        this.outPutText.setText("");
        String text = inputText.getText();
        String[] array = text.split("\r|\n|\r\n");
        for (String s : array) {
            if (s.trim().isEmpty()) {
                continue;
            }
            char c = s.charAt(0);
            c = Character.toLowerCase(c);
            s = c + s.substring(1);
            result += s + System.lineSeparator();
        }
        this.outPutText.setText(result);
    }//GEN-LAST:event_jButton4ActionPerformed

    private void jButton5ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton5ActionPerformed
        // TODO add your handling code here:
        this.outPutText.setText("");
        String text = inputText.getText();
        String[] array = text.split("\r|\n|\r\n");
        String qianZhui = this.qianzhuiText.getText();
        String houZhui = this.houZhuiText.getText();
        for (String s : array) {
            if (s.trim().isEmpty()) {
                continue;
            }
            String line = qianZhui + s + houZhui;
            this.outPutText.append(line + System.lineSeparator());
        }
    }//GEN-LAST:event_jButton5ActionPerformed

    private void jButton7ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton7ActionPerformed
        // TODO add your handling code here:
        String split = this.textBoxSplit.getText();
        if (split.isEmpty()) {
            JOptionPane.showMessageDialog(this, "分隔符不能为空");
        }
        this.outPutText.setText("");
        String text = inputText.getText();
        String[] columns = this.textSplitColumns.getText().split(",");
        String[] array = array = text.split("\r|\n|\r\n");
        for (String line : array) {
            String[] lineArray = line.split(split);
            for (String c : columns) {
                int cIndex = Integer.valueOf(c.trim()) - 1;
                if (cIndex >= lineArray.length) {
                    this.outPutText.append(" \t");
                } else {
                    this.outPutText.append(lineArray[cIndex] + "\t");
                }
            }
            this.outPutText.append(System.lineSeparator());
        }
    }//GEN-LAST:event_jButton7ActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.ButtonGroup buttonGroup1;
    private javax.swing.JTextField clazzNameText;
    private javax.swing.JTextField collectionNameText;
    private javax.swing.JTextField houZhuiText;
    private javax.swing.JTextArea inputText;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JButton jButton4;
    private javax.swing.JButton jButton5;
    private javax.swing.JButton jButton7;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTabbedPane jTabbedPane1;
    private javax.swing.JTabbedPane jTabbedPane2;
    private javax.swing.JRadioButton noTrimBox;
    private javax.swing.JTextArea outPutText;
    private javax.swing.JTextField qianzhuiText;
    private javax.swing.JRadioButton replaceAllBox;
    private javax.swing.JTextField textBoxSplit;
    private javax.swing.JTextField textSplitColumns;
    private javax.swing.JRadioButton trimBox;
    // End of variables declaration//GEN-END:variables

    private String getStringReuslt(boolean listvar) {
        String result = "";
        String text = inputText.getText();
        String[] array = text.split("\r|\n|\r\n");
        for (String s : array) {
            if (s.trim().isEmpty()) {
                continue;
            }
            if (trimBox.isSelected()) {
                s = s.trim();
            } else if (replaceAllBox.isSelected()) {
                s = s.replaceAll("\\s|\t", "");
            }
            s = s.replaceAll("\"", "\\\"");
            if (!listvar) {
                result += "\"" + s + "\",";
            } else {
                String list = this.collectionNameText.getText().trim();
                s = "\"" + s + "\"";
                list = list + ".add(" + s + ");";
                result += list;
            }
            result += System.lineSeparator();
        }
        return result;
    }

    private String getObjectResult(boolean listvar) {
        String text = inputText.getText();
        String[] array = text.split("\r|\n|\r\n");
        String result = "";
        for (int i = 0; i < array.length; i++) {
            String l = array[i];
            l = l.replaceAll("\"", "\\\"");
            if (l.trim().isEmpty()) {
                continue;
            }
            String[] columns = l.trim().split("\t");
            String clazz = this.clazzNameText.getText().trim();
            String line = "";
            for (String s : columns) {
                if (trimBox.isSelected()) {
                    s = s.trim();
                } else if (replaceAllBox.isSelected()) {
                    s = s.replaceAll("\\s|\t", "");
                }
                line += ",\"" + s + "\"";
            }
            if (!listvar) {
                String rowText = clazz + " item" + i + " = new " + clazz + "({0})";
                String bean = MessageFormat.format(rowText, line.substring(1));
                result += bean + ";";
            } else {
                String rowText = "new " + clazz + "({0})";
                String bean = MessageFormat.format(rowText, line.substring(1));
                String list = this.collectionNameText.getText().trim();
                list = list + ".add(" + bean + ");";
                result += list;
            }
            result += System.lineSeparator();
        }
        return result;
    }
}
